/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   MaintenanceMonyServiceImpl.java for com.pengtu.gsj.service.impl.check.jhgl    </p>
 * 
 * <p>最后修改 : $: 2017年7月12日-下午2:55:39 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jhgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jhgl.MaintenanceMonyDao;
import com.pengtu.gsj.dao.jhgl.MoneyDetailDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jhgl.MoneyDetail;
import com.pengtu.gsj.entity.jhgl.MoneyOverView;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jhgl.MaintenanceMonyService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 国省检-计划管理模块养护资金实现层
 * MaintenanceMonyServiceImpl
 * 
 * 2017年7月12日 下午2:55:39
 * 
 * @version 1.0.0
 * 
 */
@Transactional
@Service
public class MaintenanceMonyServiceImpl implements MaintenanceMonyService {
	
	@Autowired
	private MaintenanceMonyDao maintenanceMonyDao;
	
	@Autowired
	private MoneyDetailDao moneyDetailDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Override
	public List<MoneyOverView> getMoneyOverViewDatas(QueryFilter queryFilter){
		return maintenanceMonyDao.find(queryFilter);
	}
	
	@Override
	public List<MoneyDetail> getMoneyDetailDatas(QueryFilter queryFilter){
		return moneyDetailDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getMoneyOverView(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<MoneyOverView> overViewList = new ArrayList<MoneyOverView>();
		List<MoneyDetail> detailList = new ArrayList<MoneyDetail>();
		List<Object> values = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//年度安排不少于500元/公里得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//年度安排少于500元/公里得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//资金项目每减少一个百分点扣分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//是否预留应急抢险资金得分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		String projectIds = (String)jsonObject.get("projectIds");
		//取到所有文件
		List<Attachment> list = systemManageService.getCheckAttachmentsByPartId(startTime, endTime, tag, projectIds);
		
		//养护资金项目的名称
		QueryFilter queryFilter = new QueryFilter();
		//年度养护资金详情查询
		queryFilter.addFilter("year", startTime, MatchType.GE);
		queryFilter.addFilter("year", endTime, MatchType.LE);
		detailList = this.getMoneyDetailDatas(queryFilter);
		double total =0; //检查得分
		double percentage =0; //系统完善度
		
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(startTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double tableFull = MathsUtils.mulDouble(yearFull, 2*years.length);
		tableFull = MathsUtils.sumDouble(tableFull, MathsUtils.mulDouble(reserveMark, 2));
		tableFull  = 15+3+tableFull;	//满分
		//项目资金总览()
		queryFilter = new QueryFilter();
		if (StringUtils.isEmpty(projectIds)) {
			overViewList = this.getMoneyOverViewDatas(queryFilter);
		} else {
			queryFilter.addFilter("id", projectIds, MatchType.EQ);
			overViewList = this.getMoneyOverViewDatas(queryFilter);
			//计算得分详情和资料完善度
			//资金项目使用比例得分
			for (MoneyOverView moneyOverView : overViewList) {
				if (StringUtils.isNotEmpty(moneyOverView.getUsePate().toString())) {
					double overMark = MathsUtils.subDouble(100, moneyOverView.getUsePate());
					overMark = MathsUtils.mulDouble(overMark, poitMark);
					if (overMark >= 15) {
						total = 0.0;
					} else {
						total = MathsUtils.subDouble(15, overMark);  //资金项目得分
					}
					map.put("projectMark", total);
				}
			}
			double tempSum = 0;//年度得分和 
			//专项资金不少于500元/公里的得分
			for (int i = 0; i< detailList.size(); i++) {
				double temp = 0;        //年度得分
				//市级专项资金
				if (StringUtils.isEmpty(detailList.get(i).getCityMoney().toString())){
					total = MathsUtils.sumDouble(total, 0.0);
					temp = MathsUtils.sumDouble(temp, 0.0);
				} else if (detailList.get(i).getCityMoney() >= 500) {
					total = MathsUtils.sumDouble(total, yearFull);
					temp = MathsUtils.sumDouble(total, yearFull);
				} else {
					total = MathsUtils.sumDouble(total, yearPart);
					temp = MathsUtils.sumDouble(total, yearPart);
				}
				
				//县级专项资金
				if (StringUtils.isEmpty(detailList.get(i).getCountyMoney().toString())) {
					total = MathsUtils.sumDouble(total, 0.0);
					temp = MathsUtils.sumDouble(temp, 0.0);
				} else if (detailList.get(i).getCountyMoney() >= 500) {
					total = MathsUtils.sumDouble(total, yearFull);
					temp = MathsUtils.sumDouble(total, yearFull);
				} else {
					total = MathsUtils.sumDouble(total, yearPart);
					temp = MathsUtils.sumDouble(total, yearPart);
				}
				
				//专项资金是否增长 //1是 0否
				if ("1".equals(detailList.get(i).getIsGrow())) { 	
					total = MathsUtils.sumDouble(total, 3.0);
					temp = MathsUtils.sumDouble(temp, 3.0);
				} else {
					total = MathsUtils.sumDouble(total, 0.0);
					temp = MathsUtils.sumDouble(temp, 0.0);
				}
				
				//预留应急抢险资金
				if ("1".equals(detailList.get(i).getIsReserved())) {
					total = MathsUtils.sumDouble(total, reserveMark);
					temp = MathsUtils.sumDouble(temp, reserveMark);
				} else {
					total = MathsUtils.sumDouble(total, 0.0);
					temp = MathsUtils.sumDouble(temp, 0.0);
				}
				tempSum += temp;
				map.put("yearMark"+i, temp);
			}
			map.put("yearSumMark", tempSum);
			
			map.put("sum",  MathsUtils.formatDouble(total,2));
			//判断完善度
			tableFull = MathsUtils.formatDouble(tableFull,2);
			percentage = MathsUtils.divDouble(total *100 ,tableFull, 2) ;
			map.put("percent", percentage);
			//返回时间
			map.put("years", years);
		}
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		//页面初始化时   属性为null  会导致空指针异常
		if (StringUtils.isNotEmpty(projectIds)) {
			values.add(projectIds);
		}
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("overView", overViewList);
		map.put("detail", detailList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
